package com.magic.smartcity.view.more.hospital

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.magic.smartcity.R
import com.magic.smartcity.beans.HospitalDepartments
import com.magic.smartcity.beans.HospitalPatientCard
import com.magic.smartcity.beans.HospitalPatientReserve
import com.magic.smartcity.databinding.FragmentHospitalDepartmentNormalBinding
import com.magic.smartcity.view.more.viewmodels.HospitalViewModel
import java.util.*

class HospitalDepartmentNormalFragment(
    departmentsDetail: HospitalDepartments,
    patientCardDetail: HospitalPatientCard
) : Fragment() {
    private val departmentsDetail = departmentsDetail
    private val patientCardDetail = patientCardDetail
    private lateinit var binding: FragmentHospitalDepartmentNormalBinding
    private val viewModel by lazy {
        ViewModelProvider(this).get(HospitalViewModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FragmentHospitalDepartmentNormalBinding.inflate(layoutInflater)
        initView()
        return binding.root
    }

    //选择日期后进行执行的操作
    private val dateSetListener: DatePickerDialog.OnDateSetListener =
        DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
            val date = "${year}-${month + 1}-${dayOfMonth}"
            binding.yuyueDate.setText(date)
        }
    //选择时间后进行执行的操作
    private val timeSetListener: TimePickerDialog.OnTimeSetListener =
        TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
            val time = "${hourOfDay}:${minute}"
            binding.yuyueTime.setText(time)
        }


    private fun initView() {
        //设置页面显示数据
        binding.departmentsDetail = departmentsDetail

        //初始化日期选择
        //选择日期时，初始化默认选中的是系统当前日期
        val calendar = Calendar.getInstance()
        val selectDateLambda = {
            val datePickerDialog = DatePickerDialog(
                requireContext(),
                android.R.style.Theme_Material_Dialog_MinWidth,
                dateSetListener,
                calendar.get(Calendar.YEAR),
                calendar.get(Calendar.MONTH),
                calendar.get(Calendar.DAY_OF_MONTH)
            )
            datePickerDialog.show()
        }
        binding.yuyueDate.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) selectDateLambda()
        }
        binding.yuyueDate.setOnClickListener { selectDateLambda() }

        //初始化时间选择
        //选择时间时，初始化默认选中当前系统时间
        val selectTimeLambda = {
            val timePickerDialog = TimePickerDialog(
                requireContext(),
                android.R.style.Theme_Material_Dialog_MinWidth,
                timeSetListener,
                calendar.get(Calendar.HOUR),
                calendar.get(Calendar.MINUTE),
                true
            )
            timePickerDialog.show()
        }
        binding.yuyueTime.setOnFocusChangeListener { v, hasFocus ->
            if (hasFocus) selectTimeLambda()
        }
        binding.yuyueTime.setOnClickListener { selectTimeLambda() }

        //点击提交预约订单
        binding.submitAddReservation.setOnClickListener {
            when {
                binding.yuyueDate.text.toString().isEmpty() -> {
                    binding.yuyueDate.error = "请选择预约日期"
                }
                binding.yuyueTime.text.toString().isEmpty() -> {
                    binding.yuyueDate.error = null
                    binding.yuyueTime.error = "请选择预约时间"
                }
                else -> {
                    binding.yuyueDate.error = null
                    binding.yuyueTime.error = null
                    viewModel.submitPatientReserve(
                        departmentsDetail.id,
                          departmentsDetail.money,
                        patientCardDetail.name,
                        "${binding.yuyueDate.text.toString()} ${binding.yuyueTime.text.toString()}",
                        departmentsDetail.type
                    )
                }
            }
        }
        //监控提交预约订单成功后进行跳转到订单详情页面
        viewModel.submitReserve.observe(viewLifecycleOwner) {
            Toast.makeText(requireContext(), "预约成功!", Toast.LENGTH_SHORT).show()
            val bundle = Bundle()
            val hospitalPatientReserve = HospitalPatientReserve()
            hospitalPatientReserve.categoryName=departmentsDetail.categoryName
            hospitalPatientReserve.type = departmentsDetail.type
            hospitalPatientReserve.patientName = patientCardDetail.name
            hospitalPatientReserve.reserveTime = "${binding.yuyueDate.text.toString()} ${binding.yuyueTime.text.toString()}"
            bundle.putSerializable("reservationDetail",hospitalPatientReserve)
            Navigation.findNavController(binding.root).navigate(R.id.action_hospitalDepartmentsDetailFragment_to_hospitalReservationDetailFragment,bundle)
        }


    }

}